﻿using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Security;
using UnityEngine.UI;
using OnefallGames;

public class IAPManager : MonoBehaviour//, IStoreListener
{

	public Text[] _priceTexts;

	//IStoreController controller;
	//IExtensionProvider extensions;

	const string gameID = "com.mohammadfaisalkhan.knifethrow";

	private const string _productID1 = "coins200";
	private const string _productID2 = "coins450";
	private const string _productID3 = "coins600";
	private const string _productID4 = "coins800";




	public static IAPManager _instance;


	// Apple App Store-specific product identifier for the subscription product.
	private static string kProductNameAppleSubscription = "com.unity3d.subscription.new";

	// Google Play Store-specific product identifier subscription product.
	private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";

	void Start ()
	{
		_instance = this;
		//if (controller == null) {
		//	InitializePurchasing ();
		//}

		/*if (GameObject.FindGameObjectsWithTag ("IAP").Length > 1) {
			Destroy (gameObject);
		} else {
			DontDestroyOnLoad (gameObject);
		}*/



	}

	public void InitializePurchasing ()
	{
		// If we have already connected to Purchasing ...
		//if (IsInitialized ()) {
		//	// ... we are done here.
		//	return;
		//}
		//var builder = ConfigurationBuilder.Instance (StandardPurchasingModule.Instance ());

		//// Use your own products
		//builder.AddProduct (_productID1, ProductType.Consumable);
		//builder.AddProduct (_productID2, ProductType.Consumable);
		//builder.AddProduct (_productID3, ProductType.Consumable);
		//builder.AddProduct (_productID4, ProductType.Consumable);


		//UnityPurchasing.Initialize (this, builder);
	}

	//bool IsInitialized ()
	//{
	//	// Only say we are initialized if both the Purchasing references are set.
	//	return controller != null && extensions != null;
	//}

	/// <summary>
	/// Called when Unity IAP is ready to make purchases.
	/// </summary>
	//public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
	//{
	//	//Debug.Log("IAP: Initialized successfully");
	//	this.controller = controller;
	//	this.extensions = extensions;

	//	int i = 0;
	//	foreach (var product in controller.products.all) {
	//		//Debug.Log("---------Localized Title-----------" + product.metadata.localizedTitle);
	//		//Debug.Log("---------Localized Description-----------" + product.metadata.localizedDescription);
	//		//Debug.Log("---------Localized price-----------" + product.metadata.localizedPriceString);
	//		_priceTexts [i].text = "" + product.metadata.localizedPriceString;
	//		i++;

	//	}
	//}

	/// <summary>
	/// Called when Unity IAP encounters an unrecoverable initialization error.
	///
	/// Note that this will not be called if Internet is unavailable; Unity IAP
	/// will attempt initialization until it becomes available.
	/// </summary>
	//public void OnInitializeFailed (InitializationFailureReason error)
	//{
	//	Debug.Log ("OnInitializeFailed InitializationFailureReason:" + error);
	//}

	public void BuyProductID (string productId)
	{
		//productId = gameID + productId;
		//if (IsInitialized ()) {
		//	Product product = controller.products.WithID (productId);

		//	// If the look up found a product for this device's store and that product is ready to be sold ... 
		//	if (product != null && product.availableToPurchase) {
		//		//Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
		//		// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed 
		//		// asynchronously.
		//		controller.InitiatePurchase (product);
		//	} else {
		//		// ... report the product look-up failure situation  
		//		//Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
		//	}
		//} else {
		//	// ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or 
		//	// retrying initiailization.
		//	//Debug.Log("BuyProductID FAIL. Not initialized.");
		//}
	}

	/// <summary>
	/// Called when a purchase completes.
	///
	/// May be called at any time after OnInitialized().
	/// </summary>
//	public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e)
//	{

//		bool validPurchase = true; // Presume valid for platforms with no R.V.

//		// Unity IAP's validation logic is only included on these platforms.
//#if UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE_OSX
//		// Prepare the validator with the secrets we prepared in the Editor
//		// obfuscation window.
//		var validator = new CrossPlatformValidator (GooglePlayTangle.Data (),
//			                AppleTangle.Data (), Application.identifier);

//		try {
//			// On Google Play, result has a single product ID.
//			// On Apple stores, receipts contain multiple products.
//			var result = validator.Validate (e.purchasedProduct.receipt);
//			// For informational purposes, we list the receipt(s)
//			//Debug.Log("Receipt is valid. Contents:");
//			foreach (IPurchaseReceipt productReceipt in result) {

//				if (productReceipt.productID != e.purchasedProduct.definition.id) {

//					//Debug.Log("Invalid receipt data");
//					validPurchase = false;
//					PlayerPrefs.SetInt ("CanUseTurbo", 0);
//				}
//			}
//		} catch (IAPSecurityException) {
//			//Debug.Log("Invalid receipt, not unlocking content");
//			validPurchase = false;
//#if UNITY_EDITOR
//			validPurchase = true;
//#endif
//		}
//#endif

	//	//apply the purchasing in case if the transaction is valid
	//	if (validPurchase) {
	//		switch (e.purchasedProduct.definition.id) {
	//		//Use your own products
	//		case _productID1:
	//			CoinManager.Instance.AddCoins (200);
	//			break;
	//		case _productID2:
	//			CoinManager.Instance.AddCoins (450);
	//			break;
	//		case _productID3:
	//			CoinManager.Instance.AddCoins (600);
	//			break;
	//		case _productID4:
	//			CoinManager.Instance.AddCoins (800);
	//			break;
	//		}
	//	}

	//	return PurchaseProcessingResult.Complete;
	//}

	/// <summary>
	/// Called when a purchase fails.
	/// </summary>
	//public void OnPurchaseFailed (Product p, PurchaseFailureReason r)
	//{
	//	// A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing 
	//	// this reason with the user to guide their troubleshooting actions.
	//	Debug.Log (string.Format ("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", p.definition.storeSpecificId, r));
	//}

	// Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
	// Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
	public void RestorePurchases ()
	{
		//if (!IsInitialized ()) {
		//	Debug.Log ("RestorePurchases FAIL. Not initialized.");
		//	return;
		//}

		//// If we are running on an Apple device ... 
		//if (Application.platform == RuntimePlatform.IPhonePlayer
		//    || Application.platform == RuntimePlatform.OSXPlayer
		//    || Application.platform == RuntimePlatform.tvOS) {
		//	Debug.Log ("RestorePurchases started ...");

		//	var apple = extensions.GetExtension<IAppleExtensions> ();
		//	// Begin the asynchronous process of restoring purchases. Expect a confirmation response in 
		//	// the Action below, and ProcessPurchase if there are previously purchased products to restore.
		//	apple.RestoreTransactions (OnTransactionsRestored);
		//} else {
		//	// We are not running on an Apple device. No work is necessary to restore purchases.
		//	Debug.Log ("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
		//}
	}

	void OnTransactionsRestored (bool success)
	{
		Debug.Log ("Transactions restored " + success.ToString ());
	}
}